package com.viking.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

/**
 * @version 1.0
 * @date 2009-09-29
 */

public class FileUtil {

	public static final String SYS_POPEFILENAME = "SYS_popedom.tmp";
	public static final String SYS_AREAFILENAME = "SYS_area_manager.tmp";
	public static final String SYS_POPE_CHECK_BOX = "SYS_popedom_check.tmp";

	public static String readStream(InputStream ins) {
		try {
			InputStreamReader isr = new InputStreamReader(ins, "utf-8");
			BufferedReader reader = new BufferedReader(isr);
			StringBuilder sb = new StringBuilder();
			String line;
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
			reader.close();
			ins.close();
			return sb.toString();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "[]";
	}

	/**
	 * 根据文件名，读取文件
	 * 
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public static String readStringFromFile(String fileName) {
		String usrHome = System.getProperty("user.home");
		File f = new File(usrHome);
		if ((!f.isDirectory()) && (!f.exists())) {
			System.out.println("读取文件失败，user.home 文件路径不存在！");
			return null;
		}
		File file = new File(f, fileName);
		System.out.println("读取文件路径：" + file);
		if (!file.exists()) {
			System.out.println(file + "文件不存在");
			return null;
		}
		StringBuffer buf = new StringBuffer();
		FileReader fr = null;
		try {
			fr = new FileReader(file);
			int ch = 0;
			while ((ch = fr.read()) != -1) {
				buf.append((char) ch);
			}
			fr.close();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				if (fr != null) {
					fr.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}
		String backJson = buf.toString();
		if (backJson.length() <= 0) {
			return null;
		}
		return backJson;
	}

	/**
	 * 根据文件名将json数据写入文件
	 * 
	 * @param fileName
	 * @param json
	 * @throws IOException
	 */
	public static void writeStringToFile(String fileName, String json) {
		String usrHome = System.getProperty("user.home");
		File f = new File(usrHome);
		if ((!f.isDirectory()) && (!f.exists())) {
			System.out.println("创建文件失败，user.home 文件路径不存在！");
		}
		File file = new File(f, fileName);
		if (file.exists()) {
			file.delete();
		}
		FileOutputStream fo = null;
		try {
			file.createNewFile();
			System.out.println("创建文件成功，文件路径：" + file);
			fo = new FileOutputStream(file);
			byte[] b = json.getBytes();
			fo.write(b);
			fo.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fo != null) {
					fo.close();
				}
			} catch (IOException e) {
			}
		}
	}

	public void writeFile(String fileName, String content) {
		File file = new File(fileName);
		if (file.exists())
			file.delete();
		try {
			FileOutputStream fos = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}
